package com.misty.admin.controller.adminController;


import com.misty.admin.service.BlogService;
import com.misty.admin.service.CommentService;
import com.misty.common.bean.Result;
import com.misty.dao.domain.Blog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cthulhu
 * @since 2022-11-21
 */
@RestController
@RequestMapping("/blog")
@PreAuthorize("hasAuthority('blog')")
public class BlogController {

    @Autowired
    private BlogService blogService;

    @Autowired
    private CommentService commentService;

    @PostMapping("/save")
    public Result<?> save(@RequestBody Blog blog) {
        return blogService.saveBlog(blog);
    }

    @PutMapping("/update")
    public Result<?> update(@RequestBody Blog blog) {
        return blogService.updateBlog(blog);
    }

    @DeleteMapping("/delete")
    public Result<?> delete(@RequestParam(name = "id") Integer  blogId) {
        return blogService.deleteBlog(blogId);
    }

    @GetMapping()
    public Result<?> Page(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(defaultValue = "") String search
    ) {
        return blogService.Page(pageNum,pageSize,search);
    }

    @GetMapping("/order")
    public Result<?> order(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(defaultValue = "") String search
    ) {
        return blogService.order(pageNum,pageSize,search);
    }

    @GetMapping("/{id}")
    public Result<?> selectByIdWithUser(@PathVariable(name = "id") Integer id ){
        return blogService.selectByIdWithUser(id);
    }

    @GetMapping("/statistics")
    public Result<?> Statistics(){
        Result<?> blogResult= blogService.statistics();
        Map blogCountMap = (Map) blogResult.getData();

        Result<Map> commentResult = commentService.statistics();
        Map commentCountMap = commentResult.getData();

        blogCountMap.putAll(commentCountMap);
        return Result.success("统计成功!",blogCountMap);
    }

}


